# frozen_string_literal: true

require "spec_helper"
require "decidim/elections/test/per_question_vote_examples"

describe "Dashboard" do
  let(:user) { create(:user, :confirmed, organization:) }
  let!(:election) { create(:election, :published, :ongoing, :with_internal_users_census, :per_question, census_settings:) }
  let(:organization) { election.organization }
  let(:census_settings) do
    {
      "authorization_handlers" => authorization_handlers
    }
  end
  let(:authorization_handlers) do
    {}
  end
  let(:election_path) { Decidim::EngineRouter.main_proxy(election.component).election_path(election) }
  let(:new_election_vote_path) { Decidim::EngineRouter.main_proxy(election.component).new_election_per_question_vote_path(election_id: election.id) }
  let(:waiting_election_votes_path) { Decidim::EngineRouter.main_proxy(election.component).waiting_election_per_question_votes_path(election_id: election.id) }
  let(:receipt_election_votes_path) { Decidim::EngineRouter.main_proxy(election.component).receipt_election_per_question_votes_path(election_id: election.id) }
  let(:confirm_election_votes_path) { Decidim::EngineRouter.main_proxy(election.component).confirm_election_per_question_votes_path(election_id: election.id) }
  let(:new_election_normal_vote_path) { Decidim::EngineRouter.main_proxy(election.component).new_election_vote_path(election_id: election.id) }
  let!(:question1) { create(:election_question, :with_response_options, :voting_enabled, skip_injection: true, question_type: "single_option", election:, position: 1) }
  let!(:question2) { create(:election_question, :with_response_options, election:, skip_injection: true, position: 2) }
  let(:voter_uid) { user.to_global_id.to_s }

  def election_vote_path(question)
    Decidim::EngineRouter.main_proxy(election.component).election_per_question_vote_path(election_id: election.id, id: question.id)
  end

  before do
    switch_to_host(organization.host)
  end

  context "when the user is not logged in" do
    let(:election) { create(:election, :published, :ongoing, :with_token_csv_census, :per_question) }
    let(:voter_uid) { election.voters.first.to_global_id.to_s }

    before do
      visit election_path
    end

    it_behaves_like "a csv token per question votable election"
  end

  context "when the user is logged in" do
    before do
      login_as user, scope: :user
      visit election_path
    end

    it_behaves_like "a per question votable election"

    context "when some question has results published" do
      before do
        question1.update(published_results_at: Time.current)
      end

      it_behaves_like "a per question votable election with published results"
    end
  end

  context "when navigating through already voted questions" do
    let!(:question2) { create(:election_question, :with_response_options, :voting_enabled, election:, skip_injection: true, position: 2) }
    let!(:question3) { create(:election_question, :with_response_options, election:, skip_injection: true, position: 3) }

    before do
      login_as user, scope: :user
      visit election_path
    end

    it_behaves_like "a per question votable election with already voted questions"
  end

  context "when the election is real time" do
    let(:election) { create(:election, :published, :ongoing, :with_internal_users_census, :real_time, :with_questions) }

    it "redirects to the per question vote path" do
      visit new_election_vote_path
      expect(page).to have_current_path(new_election_normal_vote_path)
    end
  end

  context "when the election is after end" do
    let(:election) { create(:election, :published, :ongoing, :with_internal_users_census, :after_end, :with_questions) }

    it "redirects to the per question vote path" do
      visit new_election_vote_path
      expect(page).to have_current_path(new_election_normal_vote_path)
    end
  end
end
